package com.gf.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.base.controller.BasicController;
import com.gf.base.type.Role;
import com.gf.common.constant.Constants;
import com.gf.common.staticer.AboutContentHelper;
import com.gf.common.util.WebHelper;
import com.gf.user.model.User;
import com.gf.user.service.SuperService;

/**
 * 超级管理员可以招聘、解雇管理员<br>
 * 只能任命普通用户为管理员<br>
 * 医生不能升级为管理员
 * 
 * @author zhuguifeng
 * 
 */
@Controller
@RequestMapping("/super")
public class SuperController extends BasicController {

	@Autowired
	private SuperService superService;

	/**
	 * 只能任命普通用户为管理员<br>
	 * 病人和医生不能升级为管理员
	 * 
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "/authorize", method = RequestMethod.POST)
	public @ResponseBody
	boolean authorizeAdmin(long userId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}

		if (!user.getRole().toString().equals(Role.SUPER.toString())) {
			return false;
		}

		return superService.authorizeAdmin(userId);
	}

	/**
	 * 任命谁为管理员
	 * 
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "/fire", method = RequestMethod.POST)
	public @ResponseBody
	boolean fireAdmin(long userId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}

		if (!user.getRole().toString().equals(Role.SUPER.toString())) {
			return false;
		}

		boolean result = false;
		if (WebHelper.checkSuperControl()) {
			result = superService.fireAdmin(userId);
		}
		return result;
	}

	/**
	 * 更改 "关于我们"的内容<br>
	 * 需要超级管理员权限<br>
	 * 得到内容不需要权限,也不需要登入,在 /base下
	 */
	@RequestMapping(value = "/about/update", method = RequestMethod.POST)
	public @ResponseBody
	long updateAbout(String content) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return -1;
		}

		if (!user.getRole().toString().equals(Role.SUPER.toString())) {
			return -1;
		}
		long result = superService.updateAbout(content);
		if (result > 0) {
			AboutContentHelper.setContent(content);
		}

		return result;
	}

}
